﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.WindowsAzure.StorageClient;
using HollywoodHackers.Storage.Models;

namespace HollywoodHackers.Storage.Repositories
{
    public class UserTextNotificationRepository : StorageBase
    {
        public const string EntitySetName = "UserTextNotifications";

        CloudTableClient tableClient;
        UserTextNotificationContext notificationContext;

        public UserTextNotificationRepository()
            : base()
        {
            tableClient = new CloudTableClient(StorageBase.TableBaseUri, StorageBase.Credentials);
            notificationContext = new UserTextNotificationContext(StorageBase.TableBaseUri, StorageBase.Credentials);

            tableClient.CreateTableIfNotExist(EntitySetName);
        }

        public UserTextNotification[] GetNotificationsForUser(string userName)
        {
            var q = from notification in notificationContext.UserNotifications
                    where notification.TargetUserName == userName
                    select notification;

            return q.ToArray();
        }

        public void AddNotification(UserTextNotification notification)
        {
            // This is missing a decent try/catch
            notification.RowKey = Guid.NewGuid().ToString();
            notificationContext.AddObject(EntitySetName, notification);
            notificationContext.SaveChanges();
        }
    }
}
